<?php
///usr/local/php/bin/php -c /usr/local/php/etc/php.ini /data/www/esmj_script/script.php request/DiamonConsumeHandler
/**
 * Created by PhpStorm.
 * User: xiaocui
 * Date: 2018/9/30
 * Time: 15:10
 * 玩家钻石消耗统计
 */
class DiamonConsumeHandler  extends ScriptBase{
    
    private $game_db;
    
    public function logic()
    {
        set_time_limit(0);
        $this->game_db = $this->mysql('game');
        
        $this->set_data('2018-10-04');
        usleep(100000);
        $this->set_data('2018-10-05');
    }
    
    
    private function set_data($date){
        $btime = strtotime($date.' 00:00:00');
        $etime = strtotime($date.' 23:59:59');
        $sql = "insert into t_diamon_consume(create_date,uid,nickname,diamon_num)
        select '{$date}',m.i_uid,(select nickname from esmj.d_user a where a.uid = m.i_uid) as nickname,m.consume from
        (SELECT i_uid,SUM(i_diamond)AS consume
        FROM daq.daq_diamond_record b
        WHERE i_report_time BETWEEN '{$btime}' AND '{$etime}' AND i_channel_type in (1,2,3,4,5,6)
        GROUP BY i_uid) m;";
        $this->game_db->query($sql);
        
        $sql = "UPDATE t_diamon_consume m SET rank =
        (SELECT rownum
        FROM(SELECT  @rownum := @rownum + 1 AS rownum,obj.uid,obj.diamon_num
        FROM (SELECT diamon_num,uid
        FROM t_diamon_consume
        where create_date = '{$date}'
        ORDER BY diamon_num ASC,uid ASC) AS obj,
        (SELECT @rownum := 0) r ) n
        WHERE m.uid = n.uid)
        where create_date = '{$date}'";
        $this->game_db->query($sql);
    }
    
}